package com.nervenets.template.hibernate.domain.user;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.nervenets.general.annotation.DataUniqueness;
import com.nervenets.general.hibernate.DomainObject;
import com.nervenets.general.utils.AESUtils;
import com.nervenets.template.enumeration.Gender;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@ApiModel("管理人员/员工表")
@Entity
@Table
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
@DataUniqueness(fields = "mobile")
public class Manager extends DomainObject {
    @ApiModelProperty(value = "账号")
    @org.hibernate.annotations.Index(name = "_idx_manager_mobile")
    @Column(columnDefinition = "varchar(20)")
    private String mobile;
    @ApiModelProperty(value = "密码")
    @JsonIgnore
    @JSONField(serialize = false)
    private String password;
    @ApiModelProperty(value = "昵称")
    @Column(columnDefinition = "varchar(20)")
    private String name;
    @ApiModelProperty(value = "头像")
    private String avatar;
    @ManyToMany
    private List<RoleGroup> roleGroups = new ArrayList<>();
    @Builder.Default
    @ApiModelProperty(value = "性别")
    @Column(columnDefinition = "tinyint")
    private Gender gender = Gender.female;
    @Builder.Default
    @ApiModelProperty(value = "是否启用")
    private boolean enable = true;

    public String decryptPassword() {
        return StringUtils.trim(AESUtils.decryptString(this.password));
    }

    public void generateNewPassword(String password) {
        this.password = AESUtils.encryptString(password);
    }
}
